package com.situ.mall.admin.util;

import com.situ.mall.admin.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class RequestUtil {
    private static final String CURRENT = "currentMember";

    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    public static void removeSession(){
        getSession().removeAttribute("CURRENT");
    }

    //保存当前用户进session
    public static void setCurrent(User user){
        getSession().setAttribute(CURRENT,user);
    }
    public static User getAdmin() {
        Object obj=getSession().getAttribute(CURRENT);
        if(obj==null){
            return null;
        }
        return (User) obj;
    }

    public static User getCurrent() {

        return (User) getRequest().getSession().getAttribute(CURRENT);
    }
}
